package com.spdg.ring.task;

import android.content.Context;
import cn.wolf.base.BaseTask;
import cn.wolf.http.BaseResp;
import cn.wolf.http.HttpCallBack;
import cn.wolf.http.HttpRequest;
import cn.wolf.tools.AppHelper;
import cn.wolf.tools.CommonUtils;
import cn.wolf.tools.Log;
import cn.wolf.tools.StringUtil;

import com.spdg.ring.R;
import com.spdg.ring.common.Config;

/**
 * <p>Title:渠道保存异步处理       </p>
 * <p>Description: 
 *  上传渠道信息以及设备相关信息
 * </p>
 * <p>@author: Eric.wsd                </p>
 * <p>Copyright: Copyright (c) 2012    </p>
 * <p>Company: HX Co., Ltd.          </p>
 * <p>Create Time: 2012-11-12             </p>
 * <p>Update Time:                     </p>
 * <p>Updater:                         </p>
 * <p>Update Comments:                 </p>
 */
public class RecordChannelTask extends BaseTask<Void, Void, BaseResp> {

	public RecordChannelTask(HttpCallBack<BaseResp> iCall, Context context) {
		super(iCall, context);
	}

	@Override
	protected BaseResp doInBackground(Void... params) {
		HttpRequest request = new HttpRequest(BaseResp.class);

		String pid = AppHelper.getPackageName(mContext);
		String imsi = AppHelper.getMobileIMSI(mContext);
		String channel = mContext.getString(R.string.umeng_channel_id); // 渠道号
		String osVersion = AppHelper.getOSRelease(); // 系统版本号
		String appId = AppHelper.getPackageName(mContext); // 包名
		String appName = mContext.getString(R.string.app_name); // 应用名称
		String carieer = AppHelper.getSimOperatorName(mContext); // 运营商
		String brand = AppHelper.getBrand(); // 手机品牌
		String manufacturer = AppHelper.getManufacturer(); // 厂商信息
		String appVersion = String.valueOf(AppHelper.getVersionCode(mContext));// 应用版本号
		String appVersionName = AppHelper.getVersionName(mContext); // 应用版本名称
		String screenWidth = String.valueOf(AppHelper.getScreenWidth(mContext)); // 屏幕宽度
		String screenHeight = String.valueOf(AppHelper.getScreenHeight(mContext)); // 屏幕高度
		String imei = AppHelper.getIMEI(mContext);
		String accessType = CommonUtils.getAccessType(mContext); // 网络制式
		String mac = AppHelper.getSerialCode();

		request.addParameter("version", appVersion);
		request.addParameter("appid", appId);
		request.addParameter("app_name", appName);
		request.addParameter("version_name", appVersionName);
		request.addParameter("channel", channel);
		request.addParameter("imsi", imsi);
		request.addParameter("os_version", osVersion);
		request.addParameter("imei", imei);
		request.addParameter("screen_width", screenWidth);
		request.addParameter("screen_height", screenHeight);
		request.addParameter("lat", "");
		request.addParameter("lng", "");
		request.addParameter("access_type", accessType);
		request.addParameter("carieer", carieer);
		request.addParameter("manufacturer", manufacturer);
		request.addParameter("brand", brand);
		
		if (StringUtil.isEmpty(mac)) {
			mac = imei;
		}
		request.addParameter("mac", mac);
		
		request.addParameter("appid", pid);
		BaseResp resp = null;
		try {
			
			String url = Config.URL.URL_RECORD_CHANNEL;
			resp = request.execute(url);
		} catch (Exception e) {
			Log.e("上报渠道信息异常", e);
		} finally {
			if (resp == null) {
				resp = new BaseResp();
				resp.setStatus(BaseResp.ERROR);
			}
			request.release();
		}

		return resp;
	}
}
